package com.yyzw.activiti.modeler.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyzw.activiti.modeler.dao.UserMapper;
import com.yyzw.activiti.modeler.model.DataGridRespVo;
import com.yyzw.activiti.modeler.model.ResponseVo;
import com.yyzw.activiti.modeler.model.SysRole;
import com.yyzw.activiti.modeler.model.SysUser;
import com.yyzw.activiti.modeler.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public SysUser userlogin(String userCode) {
        Example example =new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userCode",userCode);
        List<SysUser> sysUsers = this.userMapper.selectByExample(example);
        return sysUsers.get(0);
    }

    @Override
    public boolean userRegister(SysUser sysUser) {
        int insert = this.userMapper.insert(sysUser);
        return insert>0 ? true:false;
    }

    @Override
    public DataGridRespVo getSysUserByPage(int pageNum, int rows) {
        Example example =new Example(SysUser.class);
        List<SysUser> sysUsers = this.userMapper.selectByExample(example);
        DataGridRespVo dataGridRespVo = new DataGridRespVo();
        PageHelper.startPage(pageNum,rows);
        PageInfo<SysUser> pageInfo = new PageInfo<>(sysUsers);
        dataGridRespVo.setRows(sysUsers);
        dataGridRespVo.setTotal(pageInfo.getTotal());
        return dataGridRespVo;
    }

    @Override
    public ResponseVo addSysUser(SysUser sysUser) {
        int insert = this.userMapper.insert(sysUser);
        if(insert>0){
            return new ResponseVo(true,"添加成功");
        }
        return new ResponseVo(false,"添加失败");
    }

    @Override
    public int delSysUser(int userId) {
        Example example =new Example(SysUser.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        return this.userMapper.deleteByExample(example);
    }

    @Override
    public ResponseVo updateSysUser(SysUser sysUser) {
        int updateResult = this.userMapper.updateByPrimaryKey(sysUser);
        if(updateResult>0){
            return new ResponseVo(true,"修改成功");
        }
        return new ResponseVo(false,"修改失败");
    }
}
